﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;

using Gift.Model.crm.eNum;
using Gift.DAL.crm;
using Gift.Model.crm;

using ExtAspNet;

namespace WebSite.crm
{
    public class BaseCrm : System.Web.UI.Page
    {
        readonly Gift.DAL.adminlogin theAdminLogin = new Gift.DAL.adminlogin();
        readonly t_DALUserRuleRef theUserRuleRef = new t_DALUserRuleRef();


        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            if (!IsPostBack)
            {
                this.Session["CrmAdminUserId"] = "1";
                if (PageManager.Instance != null)
                {
                  
       
                        string themeValue ="access";
                        PageManager.Instance.Theme = (Theme)Enum.Parse(typeof(Theme), themeValue, true);
           
                }
            }

        }

        public void ValidateLoginRule(eRule oRule)
        {
            int iAdminId = 1;// theAdminLogin.GetIdByName(this.Session["ErpAdminlogin"].ToString());
            DataSet ds;
            bool bValidate = false;
            ds = theUserRuleRef.GetListByAdminId(iAdminId);
            if (ds != null && ds.Tables.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    if (dr["Name"].ToString() == oRule.ToString())
                    {
                        bValidate = true;
                        break;
                    }
                }
            }

    
            if (!bValidate)
            {
                base.Response.Write("<script language=javascript>window.top.location.href='/admin/login.aspx';</script>");
                base.Response.End();
            }
        }


        public string LoginAdminId
        {
            get
            {
                if (this.Session["CrmAdminUserId"] != null)
                {
                    return this.Session["CrmAdminUserId"].ToString();
                }
                else
                {
                    return null;
                }
            }
        }

        public string CompanyId
        {
            get
            {
                if (this.Session["CrmCompanyId"] != null && this.Session["CrmCompanyId"].ToString() != "")
                {
                    return this.Session["CrmCompanyId"].ToString();
                }
                else
                {
                    return null;
                }
            }
            set
            {
                this.Session["CrmCompanyId"] = value;
            }
        }

        public string AdminUser
        {
            get
            {

                if (this.Session["CrmAdminlogin"] != null)
                {
                    return this.Session["CrmAdminlogin"].ToString();
                }
                else
                {
                    return null;
                }
            }
        }

        public string CompanyLevelId
        {
            get
            {
                if (this.Session["CrmCompanyLevelId"] != null)
                {
                    return this.Session["CrmCompanyLevelId"].ToString();
                }
                else
                {
                    return "0";
                }
            }
            set
            {
                this.Session["CrmCompanyLevelId"] = value;
            }
        }

    }
}